In [5]:
import pandas as pd
#%matplotlib inline
import matplotlib.pyplot as plt
In [6]:
vis = pd.DataFrame({
'cells': [116,117,120,1,52,79,109,27,85,51,78,55,26,39,107],
'photo': [60,67,64,8,13,63,63,2,46,27,43,24,10,28,56]})
In [7]:
graph = []
def graph_all(graph): [g() for g in graph]
In [8]:
def scatter(): return plt.scatter(x=vis.cells, y=vis.photo, color='g')
graph = [scatter]
graph_all(graph)
In [9]:
x_range = vis.cells.min(), vis.cells.max()
y_mean = vis.photo.mean(), vis.photo.mean()
def mean(): return plt.plot(x_range, y_mean, color="brown")
graph.append(mean)
graph_all(graph)
The equations for the slope of the regression line can be constructed as:
In [10]:
slope = vis.cells.cov(vis.photo)/ vis.cells.var()
slope
Out[10]:
The equation for the intercept of the regression line can be construted as:
In [11]:
intercept = vis.photo.mean() - (slope * vis.cells.mean())
intercept
Out[11]:
So we can construction the regression line by solving for our expected y:
In [12]:
vis.expected = (vis.cells * slope) + intercept
vis.expected
Out[12]:
In [13]:
def regression(): return plt.plot(vis.cells, vis.expected)
graph.append(regression)
graph_all(graph)
SStotal:
In [14]:
def SStotal(): return [plt.plot((vis.cells[i], vis.cells[i]), (vis.photo.mean(), vis.photo[i]), color='black') for i in range(len(vis))]
graph.append(SStotal)
In [15]:
graph_all(graph)
SSregrssion
In [16]:
def SSreg(): return [plt.plot((vis.cells[i], vis.cells[i]), (vis.expected[i], vis.photo[i]), color='red') for i in range(len(vis))]
graph.append(SSreg)
graph_all(graph)
recall that to calculate SStotal we use the formula:
In [17]:
sum_of_squares = {'total' : ((vis.photo - vis.photo.mean())**2).sum()}
sum_of_squares['total']
Out[17]:
In [18]:
sum_of_squares['regression'] = ((vis.photo - vis.expected)**2).sum()
sum_of_squares['regression']
Out[18]:
now r squared is just..
In [19]:
r_squared = 1 - (sum_of_squares['regression']/sum_of_squares['total'])
In [20]:
r_squared
Out[20]:
In [26]:
def video(fname, mimetype):
"""Load the video in the file `fname`, with given mimetype, and display as HTML5 video.
"""
from IPython.display import HTML
video_encoded = open(fname, "rb").read().encode("base64")
video_tag = '<video controls alt="test" src="data:video/{0};base64,{1}">'.format(mimetype, video_encoded)
return HTML(data=video_tag)
In [27]:
video('animation.mp4', 'x-m4v')
Out[27]: